package com.earnings.okhttputils.utils.ui.fragment.transaction;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import com.earnings.R;
import com.earnings.okhttputils.utils.ui.BaseFragment;
import com.earnings.okhttputils.utils.ui.activity.common.StatusPriceActivity;
import com.earnings.okhttputils.utils.ui.activity.user.UserBalanceRechargeActivity;
import com.earnings.okhttputils.utils.ui.dialog.ChooseDialog;
import com.earnings.okhttputils.utils.ui.dialog.PaymentDialog;
import com.earnings.okhttputils.utils.utils.PayUtil;

/**
 * 在线充值
 * Created by HOHO on 2017/4/11.
 */

public class RechargeOnlineFragment extends BaseFragment {


    private EditText price;
    private PaymentDialog dialog;
    private int max;

    @Override
    protected void init() {
        price= (EditText) mView.findViewById( R.id.price );
        mView.findViewById( R.id.sumbit ).setOnClickListener( this );
        max=((UserBalanceRechargeActivity)getActivity()).rechargeData.getMax();
        price.addTextChangedListener( new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(price.getText().length()==0)return;
             if (Float.parseFloat( price.getText().toString())>max){
                 price.setText(max+"");
                 final ChooseDialog info =new ChooseDialog(getActivity(),((UserBalanceRechargeActivity)getActivity()).rechargeData.getMsg());
                 info.setSure( "确认", new View.OnClickListener() {
                     @Override
                     public void onClick(View v) {
                               info.dismiss();
                         ((UserBalanceRechargeActivity)getActivity()).showRechargeOffline();
                     }
                 } );
                 info.show();
             }
            }
        } );
    }

    @Override
    protected int onCreateLayout() {
        return R.layout.fragment_recharge_online;
    }



    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.sumbit) {
            String money = price.getText().toString();
            if (money.length() == 0) {
                showToast( "请输入您要充值的金额" );
                return;
            } else if (Float.parseFloat( money ) == 0) {
                showToast( "请输入您要充值的金额" );
                return;
            }


            new PayUtil(getActivity(),price.getText().toString()) {
                @Override
                public void onSuccess(String paytype, String info) {
                    bundleData.putString( "title","在线充值" );
                    bundleData.putString( "info","" );
                    bundleData.putString( "status","充值成功" );
                    bundleData.putString( "price",info );
                    skip( StatusPriceActivity.class );
                    getActivity().finish();
                }


            }.showRechargeOnline();


        }
    }



}
